<?php

/**
 * This is the model class for table "Template".
 *
 * The followings are the available columns in table 'Template':
 * @property integer $ID_TEMPLATE
 * @property integer $NU_ANO
 * @property integer $NU_SEMESTRE
 * @property string $DT_INI
 * @property string $DT_FIM
 * @property string $TX_NOME
 */
class Template extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return Template the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'Template';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('NU_ANO, NU_SEMESTRE, DT_INI, DT_FIM', 'required'),
            array('ID_TEMPLATE, NU_ANO, NU_SEMESTRE', 'numerical', 'integerOnly' => true),
            array('TX_NOME', 'length', 'max' => 50),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('NU_ANO, NU_SEMESTRE, DT_INI, DT_FIM, TX_NOME', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'disciplina_cargas' => array(self::HAS_MANY, 'Disciplina_Carga', 'ID_TEMPLATE'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'ID_TEMPLATE' => 'Modelo',
            'NU_ANO' => 'Ano',
            'NU_SEMESTRE' => 'Semestre',
            'DT_INI' => 'Início',
            'DT_FIM' => 'Fim',
            'TX_NOME' => 'Nome',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $alias = $this->getTableAlias();
        
        $criteria = new CDbCriteria;
        
        $criteria->with = array('disciplina_cargas');

        $criteria->compare($alias.'.ID_TEMPLATE', $this->ID_TEMPLATE);

        $criteria->compare('NU_ANO', $this->NU_ANO);

        $criteria->compare('NU_SEMESTRE', $this->NU_SEMESTRE);

        $criteria->compare('DT_INI', $this->DT_INI, true);

        $criteria->compare('DT_FIM', $this->DT_FIM, true);

        $criteria->compare('TX_NOME', $this->TX_NOME, true);

        return new CActiveDataProvider('Template', array(
                    'criteria' => $criteria,
                ));
    }

    public function afterFind() {
        parent::afterFind();
        $this->DT_INI = MethodUtils::getDateFromTableFormat($this->DT_INI);
        $this->DT_INI = substr($this->DT_INI, 0, 5);
        $this->DT_FIM = MethodUtils::getDateFromTableFormat($this->DT_FIM);
        $this->DT_FIM = substr($this->DT_FIM, 0, 5);
    }

}